package com.berwin.syncer.views;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;

public class UploadView extends JFrame {

    private String fileName;

    private JProgressBar progressBar;
    private JLabel lblProgress;

    public UploadView(String fileName) {
        this.fileName = fileName;
        this.setTitle("文件传输中...");
        this.setSize(400, 60);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.init();
    }

    private void init() {
        this.setLayout(new BorderLayout());

        progressBar = new JProgressBar();
        this.add(progressBar, BorderLayout.CENTER);

        JPanel pnlSouth = new JPanel(new FlowLayout(FlowLayout.CENTER));
        this.add(pnlSouth, BorderLayout.SOUTH);
        lblProgress = new JLabel(String.format("%s - %d%%", fileName, 0));
        pnlSouth.add(lblProgress);

    }

    public void setProgress(int value) {
        this.progressBar.setValue(value);
        this.lblProgress.setText(String.format("%s - %d%%", fileName, value));
    }

    @Override
    protected void processWindowEvent(WindowEvent e) {
        if (e.getID() == WindowEvent.WINDOW_CLOSING && this.progressBar.getValue() < 100) {
            return;
        }
        super.processWindowEvent(e);
    }
}
